package Sever;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import libarary.Contants;

class ThreadSocket extends Thread implements Contants {
	Socket socket;
	Server server;
	DataOutputStream dos;
	DataInputStream din;
	public static User us;
	public int playge=0;
	public boolean loop = true;
	int ip;

	public ThreadSocket(Server server, Socket pSocket) {
		this.socket = pSocket;
		ip = pSocket.getPort();
		this.server = server;
		us = new User();
	}

	public User getUser() {
		return us;
	}

	public void open() {
		try {
			dos = new DataOutputStream(socket.getOutputStream());
			din = new DataInputStream(socket.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void sendData(String st) {
		try {
			if (socket != null) {
				dos.writeUTF(st);
				dos.flush();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public String receiveData() {

		try {
			if (din != null)
				return din.readUTF().toString();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public String ChekLogin(String st) {

		User tg = new User();
		st = st.substring(1);
		String name = st.substring(0, st.indexOf(state.noichuoi));

		String ps = st.substring(
				st.indexOf(state.noichuoi) + state.noichuoi.length());

		us = tg.Login(name, ps);
		if (us != null) {
			boolean kq = us.updateOnl(1);
			if (kq)
				return name;
		}
		return state.error;
	}

	public void chekExit() {
		us = new User(this.getName());
		boolean kq = us.updateOnl(0);
		
	}

	public int checkReceiveData(String st) {
		if (st.equals("") != true) {
			char tv = st.charAt(0);
			switch (tv) {
			case '0':
				return state.Login;
			case '1':
				return state.danhsach;
			case '2':
				return state.thoat;
			case '3':
				return state.update;
			case '4':
				return state.chat;
			case '7':
				return state.playgame;
			case '8':
				return state.gameover;
			case '9':
				return state.gamescore;
			case 't':
				return 10;
			case 'o':
				return 11;
			}
		}
		return -1;
	}
	public void run() {
		try {

			while (loop) {
				
					String time = din.readUTF().toString();
					String threaname = this.getName();
					int task = checkReceiveData(time);
					//System.out.print(time);
					switch (task) {
					case state.thoat:
						chekExit();
						server.thoat(threaname);
						socket.close();
						loop=false;
						System.out.print("thoat");
						break;
					case state.Login:
						String kq=ChekLogin(time);
						if(kq.equals(state.error))
						{
							sendData(state.Login+kq);
						}
						else{
							this.setName(kq);
							server.login(kq);
						}
						break;
					case state.chat:
						String chat=time.substring(1);
						server.chatRoom(chat);
						break;
					case state.update:
						break;
					case state.playgame:
							server.playgame(this.getName());
						break;
					case state.gameover:
						
						
						break;
					case state.gamescore:
						server.updatescore();
						break;
					case 10:
						server.timemer(this.getName());
						break;
					case 11:
						time=time.substring(1);
						time=time.trim();
						playge=0;
						server.gameover(this.getName(),time);
						break;
					default :
						server.sendata(time, threaname);
				} 
				// System.out.print(threaname);
				// server.sendata(Task(time),threaname);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
